(function(){
    _$(document).ready(function(){
        function App(){};
        function excludeSpecial(s) {
            // 去掉转义字符
            s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
            // 去掉特殊字符
            s = s.replace(/[\@\#\$\%\^\&\*\{\}\:\"\L\<\>\?]/);
            return s;
        };
        var page=0;
        var page1=0;
        App.prototype.ajaxFn=function(){
            //获取消息
            _$.ajax({
                type: 'post',
                url: '/getMsgUserList',
                data: {},
                async: false,
                success: function (data) {
                    console.log(data);
                    if (data.code == 1) {
                        var info = data.info.data;
                        var brimg = data.info.userInfo.userimg;
                        var brnick = data.info.userInfo.usernick;
                        var bookArr = data.info.bookArr;
                        var brforum = data.info.brforum;
                        var bruser = data.info.bruser;
                        if (info == "") {
                            _$(".ui-loading-block").fadeOut();
                        } else {
                            for (var i = 0; i < info.length; i++) {
                                info[i].imgurl = brimg[i];
                                info[i].brname = bruser[i]['nickname'];
                                info[i].bruid = bruser[i]['uid'];
                                info[i].brbookName = bookArr[i]['bName'];
                                info[i].brcnt = brforum[i]['cnt'];
                                info[i].nickname = brnick[i];
                            }
                            console.log(info);
                            if (info.length > 0) {
                                var html = "";
                                for (var val in info) {
                                    info[val]['startTime'] = getTimes(info[val]['startTime']);
                                    html += template('reviewtpl', info[val]);
                                }
                                // 为了测试，延迟1秒加载
                                setTimeout(function () {
                                    _$(".ui-loading-block").fadeOut();
                                    _$(".null-x").remove();
                                    _$("#postListContainer").html(html);
                                }, 1000);
                            }else{
                                _$("#postListContainer").html('<div class="null-x"><div class="null">暂无消息</div></div>');
                            }
                        }
                    }
                }
            });
            //获取评论
            _$.ajax({
                type: 'post',
                url: '/getMsgUserCount',
                data: {},
                async: false,
                success: function (json) {
                    var dropload = $('#pinglun').dropload({
                        scrollArea: window,
                        domDown: {
                            domClass: 'dropload-down',
                            domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                            domNoData: '<div class="dropload-noData">已显示全部数据</div>'
                        },
                        //上拉
                        loadDownFn: function (me) {
                            var pageLength = json.count;
                            console.log(pageLength);
                            if (json.count % 10 > 0) {
                                pageLength = (json.count / 10) + 1;
                            }
                            if (page > pageLength) {
                                me.lock();
                                me.noData();
                                me.resetload();
                            } else {
                                console.log(page);
                                _$.ajax({
                                    type: 'post',
                                    url: '/getMsgUserPinglun',
                                    data: {page: page},
                                    async: false,
                                    success: function (data) {
                                        console.log(data);
                                        if (data.code == 1) {
                                            var info = data.info.data;
                                            var userInfo = data.info.userinfo;
                                            var bookArr = data.info.bookArr;
                                            if (info == "") {
                                                me.lock();
                                                me.noData();
                                                me.resetload();
                                            } else {
                                                var html = "";
                                                for (var val in info) {
                                                    info[val].imgurl = userInfo['imgurl'];
                                                    info[val].brbookName = bookArr[val]['bName'];
                                                    info[val].nickname = userInfo['nickname'];
                                                    info[val]['startTime'] = getTimes(info[val]['startTime']);
                                                    html += template('tpl', info[val]);
                                                }
                                                setTimeout(function () {
                                                    // 插入加载使用 html() 重置 DOM
                                                    _$("#pinglun .dropload-down").before(html);
                                                    page++;
                                                    // 每次数据加载完，必须重置
                                                    me.resetload();
                                                }, 1000);
                                            }
                                        }else if(data.code==0){
                                            _$("#pinglun").html('<div class="null-x"><div class="null">暂无评论</div></div>');
                                            // 每次数据加载完，必须重置
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }else {
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            });
            //获取回复
            _$.ajax({
                type: 'post',
                url: '/getMsgUserSubCount',
                data: {},
                async: false,
                success: function (json) {
                    var dropload = $('#huifu').dropload({
                        scrollArea: window,
                        domDown: {
                            domClass: 'dropload-down',
                            domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                            domNoData: '<div class="dropload-noData">已显示全部数据</div>'
                        },
                        //上拉
                        loadDownFn: function (me) {
                            var pageLength = json.count;
                            console.log(pageLength);
                            if (json.count % 10 > 0) {
                                pageLength = (json.count / 10) + 1;
                            }
                            if (page1 > pageLength) {
                                me.lock();
                                me.noData();
                                me.resetload();
                            } else {
                                console.log(page1);
                                _$.ajax({
                                    type: 'post',
                                    url: '/getMsgUserHuifu',
                                    data: {page: page},
                                    async: false,
                                    success: function (data) {
                                        console.log(data);
                                        if (data.code == 1) {
                                            var info = data.info.data;
                                            var userInfo = data.info.userinfo;
                                            var bookArr = data.info.bookArr;
                                            if (info == "") {
                                                me.lock();
                                                me.noData();
                                                me.resetload();
                                            } else {
                                                var html = "";
                                                for (var val in info) {
                                                    info[val].imgurl = userInfo['imgurl'];
                                                    info[val].brbookName = bookArr[val]['bName'];
                                                    info[val].nickname = userInfo['nickname'];
                                                    info[val]['startTime'] = getTimes(info[val]['startTime']);
                                                    html += template('tpl', info[val]);
                                                }
                                                setTimeout(function () {
                                                    // 插入加载使用 html() 重置 DOM
                                                    _$("#huifu .dropload-down").before(html);
                                                    page1++;
                                                    // 每次数据加载完，必须重置
                                                    me.resetload();
                                                }, 1000);
                                            }
                                        }else if(data.code==0){
                                            _$("#huifu").html('<div class="null-x"><div class="null">暂无评论</div></div>');
                                            // 每次数据加载完，必须重置
                                        }else {
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            });
        };
        App.prototype.eventFn=function(){
            _$(".ui-tab-nav li").click(function(){
                var index = _$(this).index();
                _$(this).addClass('current').siblings('li').removeClass('current');
                _$(".ui-tab-content .hide").eq(index).show().siblings('.hide').hide();
            })
        };
        App.prototype.init=function(){
            //事件调用
            this.ajaxFn();
            this.eventFn();
        };
        //程序初始化
        new App().init();
    })
})();